package com.lianxi.service.serviceImpl;

import com.lianxi.mapper.LoginMapper;
import com.lianxi.mapper.RolesMapper;
import com.lianxi.mapper.UserMapper;
import com.lianxi.pojo.Role;
import com.lianxi.pojo.User;
import com.lianxi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LoginMapper loginMapper;

    @Autowired
    private RolesMapper rolesMapper;

    @Override
    public boolean deleteUser(Integer id) {
        int i = userMapper.deleUserRolesById(id);
        if (i > 0) {
            userMapper.deleteUserById(id);
        } else {
            return false;
        }
        return true;
    }

    @Override
    public List<User> getAllUsers(String nickname) {
        List<Role> allRoles = rolesMapper.getAllRoles();
        return userMapper.getAllUsers(nickname);
    }

    @Override
    public int updateEnable(Integer uid) {
        User user = loginMapper.getUserById(uid);
        if (user.isEnabled()) {
            return userMapper.updateEnable(uid,false);
        } else {
            return userMapper.updateEnable(uid, true);
        }
    }

    @Override
    public List<Role> getAllRoles() {
        return rolesMapper.getAllRoles();
    }

    @Override
    public int doUserToRoles(Integer uid, int[] roles) {
        int result = userMapper.deleUserRolesById(uid);
        return userMapper.doUserToRoles(uid,roles);
    }
}
